package com.xone.xapp;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class LoginActivity extends BasicActivity {
	
	protected IdentifyCodeLoader imageLoader;
	protected ImageView identifyImage;
	protected EditText editUsername;
	protected EditText editPassword;
	protected EditText editIdentify;

	@Override
	public Context getContext() {
		return this;
	}
	@Override
	public String getHeaderTitle() {
		return "用户登录";
	}
	/**
	 * 是否需要检测登录
	 * @return
	 */
	@Override
	public boolean isLoginCheck() {
		return false;
	}
	@Override
	public int getContentView() {
		return R.layout.login;
	}
//	@Override
//	public void leftButton(Button leftButton) {
//		leftButton.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				LoginActivity.this.finish();
//			}
//		});
//	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		imageLoader = new IdentifyCodeLoader(this);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		identifyImage = (ImageView)findViewById(R.id.identify_code_image);
		editUsername = (EditText)findViewById(R.id.username);
		editPassword = (EditText)findViewById(R.id.password);
		editIdentify = (EditText)findViewById(R.id.identifyCode);
		editUsername.setText(ShareUtils.getPrefs(ShareUtils.USERNAME, this));
//		editPassword.setText("111111");
		imageLoader.displayImage(identifyImage);
		View identify = findViewById(R.id.view_change_identify_image);
		identify.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageLoader.displayImage(identifyImage);
				editIdentify.setText("");
			}
		});
		Button login = (Button)findViewById(R.id.button_user_login);
		login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = editUsername.getText().toString();
				String password = editPassword.getText().toString();
				String identify = editIdentify.getText().toString();
				if (StringUtils.isBlank(username)) {
					Toast.makeText(getContext(), "请输入用户名", Toast.LENGTH_LONG).show();
					return;
				}
				if (StringUtils.isBlank(password)) {
					Toast.makeText(getContext(), "请输入密码", Toast.LENGTH_LONG).show();
					return;
				}
				if (StringUtils.isBlank(identify)) {
					Toast.makeText(getContext(), "请输入验证码", Toast.LENGTH_LONG).show();
					return;
				}
				new AutoLoginDataTask(getContext()) {
					@Override
					public void loginOk(JSONObject userMap) {
						context.startActivity(new Intent(context, UserCenterActivity.class));
					}
					@Override
					public void loginFailed(String msg) {
						Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
					}
				}.execute(new String[] {
					username, password, identify
				});
			}
		});
		Button register = (Button)findViewById(R.id.button_user_register);
		register.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getContext(), RegisterActivity.class);
				startActivity(i);
			}
		});
	}
}
